package com.ymatou.iapi.fileupload.testcase;

import java.io.File;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.fileupload.service.UploadCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 上传图片 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_Upload {
	private static UploadCall uploadcall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("上传图片");
	}

	@Before
	public void caseUp() {
		uploadcall = new UploadCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(uploadcall.getOpurl());
		TestDataManager.removeTempFile();
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Upload_001() {
		Logger.start(true, "上传product图片成功,图片生成");
		try {
			String turl = "";
			String fliename = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "user2.csv";
			Logger.comment("添加上传文件:" + fliename);
			uploadcall = new UploadCall();
			uploadcall.addPart("file", fliename);

			uploadcall.callService();

			Logger.verifyEquals(ECode.SUCCESS, uploadcall.getStatus(),
					"验证Status");
			Logger.verifyEquals(ECode.PIC_UPLOAD_SUCCESS, uploadcall.getMsg(),
					"验证Msg");
			Logger.verifyNotNull(uploadcall.getFileUrl(), "验证文件地址非空");
//			turl = changeUrl(uploadcall.getFileUrl(), "");
			Logger.verifyNotNull(uploadcall.getFileUrl(), "验证文件地址非空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Upload_002() {
		Logger.start(true, "上传文件成功,图片生成");
		try {
			String turl = "";
			String fliename = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "upload.exe";
			Logger.comment("添加上传文件:" + fliename);
			uploadcall.addPart("file", fliename);

			uploadcall.callService();

			Logger.verifyEquals(ECode.SUCCESS, uploadcall.getStatus(),
					"验证Status");
			Logger.verifyEquals(ECode.PIC_UPLOAD_SUCCESS, uploadcall.getMsg(),
					"验证Msg");
			Logger.verifyNotNull(uploadcall.getFileUrl(), "验证文件地址非空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	/**
	 * 修改url地址，本case专用 用于在原来的地址文件名后加后缀
	 * 
	 * @return 修改后的地址
	 */
	private static String changeUrl(String url, String suffix) {
		return url.substring(0, url.lastIndexOf("_")) + suffix
				+ url.substring(url.lastIndexOf("."), url.length());
	}
}